package app.qrcode.browser.chat.model.dao

import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import app.qrcode.browser.chat.model.bean.UserInfo
import app.qrcode.browser.chat.model.db.UserAccountDB

/**
 * Created by DomenCai on 2022/3/11.
 */
class UserAccountDao(context: Context) {


    val mHelper = UserAccountDB(context, "UserAccount.db", 1)

    fun addAccount(user: UserInfo) {
        val db=mHelper.writableDatabase
        val values=ContentValues().apply {
            put(UserAccountTable.COL_HXID,user.hxId)
            put(UserAccountTable.COL_NAME,user.name)
            put(UserAccountTable.COL_NICK,user.nick)
            put(UserAccountTable.COL_PHOTO,user.photo)
        }
        db.replace(UserAccountTable.TAB_NAME,null,values)
    }

    @SuppressLint("Range")
    fun getAccountByHxId(hxId: String): UserInfo? {
        val db=mHelper.writableDatabase
        val sql = "select * from ${UserAccountTable.TAB_NAME} where ${UserAccountTable.COL_HXID} =?"
        val cursor=db.rawQuery(sql, listOf(hxId).toTypedArray())
        var userInfo: UserInfo? =null
        if (cursor.moveToNext()){
            userInfo=UserInfo(cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_NAME)),
            cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_HXID)),
            cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_NICK)),
            cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_PHOTO)))
        }
        cursor.close()
        return userInfo
    }
}